﻿<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>
    <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <script src="../bootstrap/js/jquery.min.js"></script>
    <script src="../bootstrap/js/bootstrap.min.js"></script>
    <!--[if lt IE 9]>
        <script src="bootstrap/js/html5shiv.min.js"></script>
        <script src="bootstrap/js/respond.min.js"></script>
    <![endif]-->
    <script src="../config.js"></script>
    <script src="../bootstrap/js/Common.js"></script>
    <script src="../bootstrap/js/controls.js"></script>
    <script type="text/javascript">
        var types = '[{ "Text": "默认", "Value":0 }, { "Text": "开关", "Value":1 }, { "Text": "整型", "Value":2 }, { "Text": "浮点","Value":3 },  { "Text":"字符串","Value":4 }, { "Text":"对象","Value":5 }, { "Text":"数组", "Value":6}]';
        var modes = '[{ "Text": "系统变量", "Value":0 }, { "Text": "IO变量", "Value":1 }, { "Text": "内存变量", "Value":2 }]';
        var states = '[{ "Text": "正常", "Value":1 }, { "Text": "屏蔽", "Value":2 }]';
        var strategy = '[{ "Text": "不存储", "Value":0 }, { "Text": "定时存储", "Value":1 }, { "Text": "变化存储", "Value":2 }]';
        $(function () {
            var formOption = {
                items: [
                    {
                        name: "dataPointID",
                        title: "名称",
                        field: "dataPointID",
                        type: "text",
                        readonly: GetUrlParam("mode") != "1" 
                    },
                    {
                        name: "type",
                        title: "数据类型",
                        field: "type",
                        source: types,
                        type: "select"
                    },
                    {
                        name: "modifier",
                        title: "变量类型",
                        field: "modifier",
                        source: modes,
                        type: "select"
                    },
                    {
                        name: "description",
                        title: "描述",
                        field: "description",
                        type: "textarea"
                    },
                    {
                        name: "state",
                        title: "屏蔽变量数据",
                        field: "state",
                        type: "checkbox"
                    },
                    {
                        name: "writeable",
                        title: "设置变量可写",
                        field: "writeable",
                        type: "checkbox"
                    },
                    {
                        name: "defaultValue",
                        title: "默认值",
                        field: "defaultValue",
                        type: "text"
                    },
                    {
                        name: "savestrategy",
                        title: "存储策略数据",
                        field: "savestrategy",
                        type: "hidden"
                    },
                    {
                        name: "strategy",
                        title: "存储策略",
                        source: strategy,
                        type: "select"
                    },
                ]
            };
            $(".form-horizontal").form(formOption).load(host_name + "varDeclares/single?Id=" + GetUrlParam("id"),
                function () {
                    document.getElementsByName('strategy')[0].addEventListener("change", function () {
                        var timeInput = $("<div class='form-group' id='timdiv'><label for='timespan' class='col-xs-3 control-label'>时间间隔(分)</label><div class='col-xs-9'><input type='text' id='timespan' class='form-control' name='timespan' placeholder='时间间隔'></div></div>")
                        if ((this.value == 1 || this.value == 2) && $(".form-horizontal")[0].length<10)  {
                            $(".form-horizontal").append(timeInput);
                        }
                        else if (this.value == 0 ) {
                            var newInput = document.getElementById("timdiv");
                            $("#timdiv").remove();
                        }
                    });  
                });
        });
        function Save() {
            var action = GetUrlParam("mode") == "1" ? "add" : "update";
            //屏蔽变量数据
            if ($(".form-horizontal")[0][4].checked) {
                $(".form-horizontal")[0][4].value = 1;
            } else {
                $(".form-horizontal")[0][4].value = 0;
            }
            //设置变量可写
            if ($(".form-horizontal")[0][5].checked) {
                $(".form-horizontal")[0][5].value ="true";
            } else {
                $(".form-horizontal")[0][5].value ="false";
            }
            //数据点存储策略
            var strInput = $("<input  class='form-control' type='text' name='savestrategy' field='savestrategy' id='savestrategy'/>");
            if ($(".form-horizontal")[0][8].value == 1 || $(".form-horizontal")[0][8].value == 2) {
                strInput.attr("value", "{ StrategyType: 1, Value:" + document.getElementById("timespan").value + "}");
            }
            else {
                strInput.attr("value", "{ StrategyType: 0, Value:''}");
            }
            $(".form-horizontal").append(strInput);
            if (action == "add" && $("#dataPointID").val() == "") {
                alert("数据点名称不能为空！");
                return "dnone";
            }
            else {
                if (action == "update") {
                    var tmpInput = $("<input  class='form-control' type='text' name='id' field='id' id='id'/>");
                    tmpInput.attr("value", GetUrlParam("id"));
                    $(".form-horizontal").append(tmpInput);
                }
                return $(".form-horizontal").form().save(host_name + "varDeclares/" + action);
            }
        }
    </script>
</head>
<body>
    <div class="container">
        <form class="form-horizontal"></form>
    </div>
</body>
</html>